﻿using CommonExtenControl;
using CommonExtenControl.Enum;
using CommonExtenControl.Event;
using CommonExtenControl.Model;
using CustomControls.PoptipCtr;
using LogProjectManager;
using ManagementToolClient.BaseViewModels;
using ManagementToolClient.PageDeclaration;
using Microsoft.Win32;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using static System.Net.Mime.MediaTypeNames;

namespace ManagementToolClient.ViewModels
{
    public class IndexPageViewModel : BaseViewModel
    {
        private Dictionary<int, int> CurrentItemIndex { set; get; }

        #region UIProperty
        private CommonExtenControl.Model.DeviceModel _SelectDeviceModel;

        public CommonExtenControl.Model.DeviceModel SelectDeviceModel
        {
            get { return _SelectDeviceModel; }
            set { SetProperty(ref _SelectDeviceModel, value); }
        }

        private bool _IsHandSearch = false;

        public bool IsHandSearch
        {
            get
            {
                return _IsHandSearch;
            }
            set
            {
                SetProperty(ref _IsHandSearch, value);
            }
        }

        private ObservableCollection<CommonExtenControl.Model.DeviceModel> _DeviceList;

        public ObservableCollection<CommonExtenControl.Model.DeviceModel> DeviceList
        {
            get { return _DeviceList; }
            set { SetProperty(ref _DeviceList, value); }
        }

        #endregion

        #region cmd
        private DelegateCommand<int?> _PageUpdatedCommand;
        public DelegateCommand<int?> PageUpdatedCommand =>
            _PageUpdatedCommand ?? (_PageUpdatedCommand = new DelegateCommand<int?>(ExecutePageUpdatedCommand));

        /// <summary>
        /// 维护
        /// </summary>
        private DelegateCommand _MaintenanceCommand;
        public DelegateCommand MaintenanceCommand =>
            _MaintenanceCommand ?? (_MaintenanceCommand = new DelegateCommand(MaintenanceCommandMethod));

        /// <summary>
        /// 刷新
        /// </summary>
        private DelegateCommand _RefreshCommand;
        public DelegateCommand RefreshCommand =>
            _RefreshCommand ?? (_RefreshCommand = new DelegateCommand(RefreshCommandMethod));


        /// <summary>
        /// 更新
        /// </summary>
        private DelegateCommand _UpdateCommand;
        public DelegateCommand UpdateCommand =>
            _UpdateCommand ?? (_UpdateCommand = new DelegateCommand(UpdateCommandMethod));


        /// <summary>
        /// Quit
        /// </summary>
        private DelegateCommand _QuitCommand;
        public DelegateCommand QuitCommand =>
            _QuitCommand ?? (_QuitCommand = new DelegateCommand(QuitCommandMethod));

        /// <summary>
        /// Run
        /// </summary>
        private DelegateCommand _RuntCommand;
        public DelegateCommand RuntCommand =>
            _RuntCommand ?? (_RuntCommand = new DelegateCommand(RunCommandMethod));

        private void RunCommandMethod()
        {
           
        }

        /// <summary>
        /// 上传
        /// </summary>
        private DelegateCommand _UpLoadCommand;
        public DelegateCommand UpLoadCommand =>
            _UpLoadCommand ?? (_UpLoadCommand = new DelegateCommand(UpLoadCommandMethod));

   


        #endregion


        public IndexPageViewModel(IContainerExtension container) : base(container)
        {

        }


        private async void ExecutePageUpdatedCommand(int? JumpIndex)
        {
            await Task.Run(() => {
           
            });
        }



        private void MaintenanceCommandMethod()
        {
            if (this.SelectDeviceModel == null)
            {
                MessageControl.Warning("请选择要操作的设备!", "RootMessageTooken");
                return;
            }
        
        }


        private void RefreshCommandMethod()
        {
            Task.Run(() => {
                GetAllCount();
               
            });
        }


        private void UpdateCommandMethod()
        {
            if (this.SelectDeviceModel==null) 
            {
                MessageControl.Warning("请选择要操作的设备!", "RootMessageTooken");
                return;
            }
           
        }
        private void ShowDialog(string _PageView)
        { 
            DialogParameters dialogParameters = new DialogParameters();

            this.Dialog?.Show(_PageView, dialogParameters, DoDialogResult);
        }

        private void DoDialogResult(IDialogResult objDResult)
        {
          
        }

        private void QuitCommandMethod()
        {
           
        }

        private int GetCurrentOperationTypeToInt(OperationType itemType) 
        {
            int _Type = 5;
            switch (itemType)
            {
                case OperationType.Maintenance:
                    _Type = 1;
                    break;
                case OperationType.Update:
                    _Type = 2;
                    break;
                case OperationType.Stop:
                    _Type = 3;
                    break;
                case OperationType.Run:
                    _Type = 4;
                    break;
                case OperationType.Other:
                    _Type = 5;
                    break;
            }
            return _Type;
        }

      

        private void UpLoadCommandMethod()
        {
            if (this.SelectDeviceModel == null)
            {
                MessageControl.Warning("请选择要操作的设备!", "RootMessageTooken");
                return;
            }
            InstanceBase<ClientNetWorkControl>.Instance.ToSendFile();
        }
        public override void ExecuteLoadedCommand()
        {
            base.ExecuteLoadedCommand();
            RegisterEvent();
            CurrentItemIndex = new Dictionary<int, int>();
            DeviceList = new ObservableCollection<CommonExtenControl.Model.DeviceModel>();
            Task.Run(() => {
                GetAllCount();
                //GetDevListFromDB(1);
            });
        }

        public override void ExecuteUnLoadedCommand()
        {
            base.ExecuteUnLoadedCommand();
            UnRegisterEvent();
        }

        private void RegisterEvent() 
        {
            Event.GetEvent<ReloadDateEvent>().Subscribe(ReloadDateMethod);
        }
        private void UnRegisterEvent()
        {
            Event.GetEvent<ReloadDateEvent>().Unsubscribe(ReloadDateMethod);
        }

        private void ReloadDateMethod(bool obj)
        {
            
        }

        private void GetAllCount() 
        {

            PageItemsCount = Limits.FirstOrDefault();
        }

     
        private OperationType GetOperationType(int _CurrentOperationType) 
        {
            OperationType item = OperationType.Other;
            if (_CurrentOperationType == 1)
            {
                item = OperationType.Update;
            } 
            else if (_CurrentOperationType == 2)
            {
                item = OperationType.Stop;
            }
            else if (_CurrentOperationType == 3)
            {
                item = OperationType.Run;
            }
            else if (_CurrentOperationType == 4)
            {
                item = OperationType.Other;
            }
            return item;
        }

        private OperationResultType GetCurrentOperationResultTypee(int _CurrentOperationType)
        {
            OperationResultType item = OperationResultType.Other;
            if (_CurrentOperationType == 1)
            {
                item = OperationResultType.Success;
            }
            else if (_CurrentOperationType == 2)
            {
                item = OperationResultType.Error;
            }
            else if (_CurrentOperationType == 3)
            {
                item = OperationResultType.Other;
            }

            return item;
        }

        protected virtual int GetSumIndex(int _pageIndex)
        {
            int sum = 0;
            int _SumIndex = 0;
            try
            {
                CurrentItemIndex.Clear();
                int sumCunt = SumPageCount / PageItemsCount + 1;
                for (int i = 1; i <= sumCunt; i++)
                {
                    CurrentItemIndex.Add(i, _SumIndex);
                    _SumIndex += PageItemsCount;
                }


                if (CurrentItemIndex.Count > 0)
                {
                    sum = CurrentItemIndex[_pageIndex];
                }
            }
            catch (Exception ex)
            {
            }
            return sum;
        }
    }
}
